<?php
namespace Frieza\Modules\Decorationtask\Controllers;

use Doraemon\model\DecorationTask\TaskGroup;
use Doraemon\tools\ImgUrl;
use Encore\Admin\Widgets\Form;


trait BaseController
{
    private  static  $prompt_none_type = 0 ;

    //装换保存提示
    public static function convertTips($data){
        if($data['finish_prompt_json']){
            $finish_prompt_arr = json_decode($data['finish_prompt_json'] , 'true');
            if($finish_prompt_arr && is_array($finish_prompt_arr)){
                if($finish_prompt_arr['title'] && $data['finish_prompt_type'] == TaskGroup::PROMPT_TYPE_FILL_IN){
                    $finish_prompt_arr['input_title'] = $finish_prompt_arr['title'];
                    unset($finish_prompt_arr['title']);
                }
                if($finish_prompt_arr['link']){
                     $finish_prompt_arr['link'] = self::urlDecode($finish_prompt_arr['link']);
                }
                if($finish_prompt_arr['button'] && is_array($finish_prompt_arr['button'])){
                    $finish_prompt_arr['text_right'] = $finish_prompt_arr['button'][0]['text'];
                    $finish_prompt_arr['url_right'] =  self::urlDecode($finish_prompt_arr['button'][0]['link']);
                    $finish_prompt_arr['text_left'] = $finish_prompt_arr['button'][1]['text'];
                    $finish_prompt_arr['url_left'] = self::urlDecode($finish_prompt_arr['button'][1]['link']);
                }
                $data = array_merge($data , $finish_prompt_arr);
            }
        }
        return $data;
    }


    //设置提醒按钮
    public static function beforePromptTypeForm(Form $form)
    {
        $form->fieldChange('finish_prompt_type', '提醒弹窗类型', $form)
            ->options([ self::$prompt_none_type => '无',TaskGroup::PROMPT_TYPE_POP_UP => '弹窗提示', TaskGroup::PROMPT_TYPE_ALERT => 'alert', TaskGroup::PROMPT_TYPE_FILL_IN => '填写信息'])
            ->default(self::$prompt_none_type)
            ->callablePreCommon(function (Form $form) {
            })->callableMap([
                self::$prompt_none_type  => function (Form $form) {},
                TaskGroup::PROMPT_TYPE_POP_UP => function (Form $form) {
                    $form->imgUpload('pic_id', 'Banner', false, ['imgType' => ImgUrl::APPTYPE_OP, 'imgSize' => ImgUrl::IMAGE_SIZE_OP_ORI]);
                    $form->text('link', '按钮跳转链接');
                },
                TaskGroup::PROMPT_TYPE_ALERT => function (Form $form) {
                    $form->text('title', '标题')->help('输入标题')->required('不能为空');
                    $form->text('desc', '描述');

                    $form->text('text_right', '右边按钮文案')->required('不能为空');
                    $form->text('url_right', '右边按钮跳转链接');

                    $form->text('text_left', '左边按钮文案')->required('不能为空');
                    $form->text('url_left', '左边按钮跳转链接');

                },
                TaskGroup::PROMPT_TYPE_FILL_IN => function (Form $form) {
                    $form->text('input_title', '标题')->help('输入标题')->required('不能为空');
                    $form->radio('input_key', '输入单元')->options(['space' => '面积', 'budget'=> '预算'])->default('space');
                    $form->text('input_extend', '输入框单元')->required('不能为空');
                    $form->text('input_placeholder', '输入框文案');
                    ;
                },
            ]);
    }


    //设置提醒保存
    public static function setPromptTips($request , $data)
    {
        $info_arr = [];
        switch($request['finish_prompt_type']){
            case TaskGroup::PROMPT_TYPE_POP_UP:
                if($request['pic_id']){

                    $info_arr = [
                        'img_url' => ImgUrl::getUrl(ImgUrl::APPTYPE_OP , $request['pic_id'] ,ImgUrl::IMAGE_SIZE_OP_ORI),
                        'pic_id' =>$request['pic_id'],
                        'link' => $request['link'] ? self::urlFromat($request['link']) : ''
                    ];

                }
                break;
            case TaskGroup::PROMPT_TYPE_ALERT:
                if($request['title'] && $request['text_right'] && $request['text_left']){
                    $info_arr = [
                        'title' => $request['title'],
                        'desc' => $request['desc'],
                        'button' =>[
                            [
                                'text' => $request['text_right'],
                                'link'=> $request['url_right'] ?  self::urlFromat($request['url_right']) :''
                            ],
                            [
                                'text' => $request['text_left'],
                                'link' => $request['url_left'] ?  self::urlFromat($request['url_left']):'',
                            ]
                        ]
                    ];
                }
                break;
            case TaskGroup::PROMPT_TYPE_FILL_IN:
                if($request['input_title'] && $request['input_extend']) {
                    $info_arr = [
                        'title' => $request['input_title'],
                        'input_extend' => $request['input_extend'],
                        'input_placeholder' => $request['input_placeholder'],
                        'input_key' => $request['input_key']
                    ];
                }
                break;
            default:
                $info_arr = [];
                break;
        }
        $data['finish_prompt_json'] = ($info_arr && is_array($info_arr)) ? json_encode($info_arr) : '';
        $data['finish_prompt_type'] = $request['finish_prompt_type'];
        return $data;
    }


    public static function urlFromat($link){
        if($link && strpos($link,"?protego=" )){

            $link_arr =  explode("?protego=", $link);
            if($link_arr && is_array($link_arr)){
                $url_str =  urlencode(base64_encode($link_arr[1]));
                $link = $link_arr[0]."?protego=".$url_str;
            }
        }
        return $link;
    }


    public static function urlDecode($link){
        if($link && strpos($link,"?protego=")){
            $link_arr =  explode("?protego=", $link);
            if($link_arr && is_array($link_arr)){
                $url_str =  base64_decode(urldecode($link_arr[1]));
                $link = $link_arr[0]."?protego=".$url_str;
            }
        }
        return $link;

    }



}